package main;

import java.io.File;
import java.util.Map;

import utils.BlockUtils;
import utils.FileType;




public class Main {
	
	private static Map<Integer, Boolean> bitMap;
	/**
	 * @param args
	 */
	public static void main(String[] args) {
		if(args.length != 2) {
			System.out.println("This command need 2 args");
			System.exit(0);
		}
		
		String root = args[0];
		String dest = args[1];
		String separator = BlockUtils.getSeparator();
		root = BlockUtils.prepareRoot(root);
		dest = BlockUtils.prepareDest(dest, root);
		bitMap = BlockUtils.createBitMap(new File(root));
		//В начале итерации мы находимся в корне
		String[] split = dest.split(separator);
		String name = split[split.length - 1];
		File f = BlockUtils.checkDestToCatalogue(root, dest);
		if(f == null)
			System.exit(1);
		Integer number = BlockUtils.getNumberEmptyBlock(bitMap);
		if(number == null) {
			System.out.println("There is no empty block in File System");
			System.exit(1);
		}
		File emptyBlock = BlockUtils.getBlockByNumber(number, root);
		BlockUtils.writeToFile(FileType.CATALOGUE_TYPE, name, emptyBlock, 0);
		System.out.println(f.getName());
		BlockUtils.addCataloguePointer(f, number, root);
	}
}
